<template>
    <div>
        <div class="header" :class="{ 'header-hidden': isHeaderHidden }">
            <div class="header-container">
                <div class="header-l">
                    <el-image :src="require('@/assets/logo/logo.png')" class="logo"></el-image>
                    <div class="title">{{ title }}</div>
                </div>
                <div class="header-r">
                    <ul class="items">
                        <li v-for="item in frontMenus" :key="item.path" @click="$router.push(item.path)">
                            <span>{{ item.meta.title }}</span>
                        </li>
                    </ul>
                    <div class="mobile-h-r">
                        <div class="more-btn"><i :class="[showMore % 2 == 0 ? 'el-icon-s-fold' : 'el-icon-s-unfold']"
                                @click="showMore++"></i></div>
                        <transition name="slide-fade">
                            <ul v-show="showMore % 2 == 0" class="mobile-menu">
                                <li v-for="item in frontMenus" :key="item.path" @click="$router.push(item.path)">
                                    <span>{{ item.meta.title }}</span>
                                </li>
                            </ul>
                        </transition>
                    </div>
                </div>
            </div>
        </div>
        <div class="header-placeholder"></div>
    </div>

</template>

<script>

export default {
    name: 'blog-header',
    data() {
        return {
            title: process.env.VUE_APP_BLOG_TITLE,
            frontMenus: [],
            showMore: 1,
            isLogin: false,
            userInfo: {},
            isHeaderHidden: false,
            lastScrollTop: 0,
        }
    },
    mounted() {
        this.getRoutes();
        window.addEventListener('scroll', this.handleScroll);
    },
    beforeDestroy() {
        window.removeEventListener('scroll', this.handleScroll);
    },
    methods: {
        handleCommand(command) {
            if (command == '/logout') {
                this.logout();
            } else {
                this.$router.push(command);
            }
        },
        getRoutes() {
            const menus = this.$store.getters.routes;
            this.frontMenus = menus.filter(item => item.meta && item.meta.isFront && !item.hidden);
        },
        init() {

        },
        logout() {

        },
        handleScroll() {
            const currentScrollTop = window.pageYOffset || document.documentElement.scrollTop;
            if (currentScrollTop > this.lastScrollTop && currentScrollTop > 55) {
                // 向下滚动
                this.isHeaderHidden = true;
            } else {
                // 向上滚动
                this.isHeaderHidden = false;
            }
            this.lastScrollTop = currentScrollTop;
        },
    },


}

</script>

<style scoped>
.header {
    width: 100%;
    height: 55px;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .05);
    background-color: #fff;
    z-index: 2000;
    transition: all .3s ease;
    touch-action: auto;
    outline: none;
    position: fixed;
    top: 0;
}

.header-hidden {
    transform: translateY(-100%);
}

.header-container {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    background-color: #fff;
}

.header-placeholder {
    height: 55px;
}

.logo {
    height: 45px;
}

.items {
    margin-right: 60px;
}

@media screen and (min-width: 1001px) {
    .header-container {
        padding: 0 100px;
    }

    .mobile-h-r {
        display: none;
    }

    .login-card {
        display: flex;
        align-items: center;
    }

    .login-card .el-dropdown {
        margin-left: 10px;
    }

    .title {
        margin-left: 20px;
        font-size: 1rem;
        font-weight: 600;
    }
}

@media screen and (max-width: 1000px) {
    .header-container {
        padding: 0 20px;
    }

    .items {
        display: none;
    }

    .title {
        margin: 0 20px;
        font-size: 1.1rem;
    }

    .login-card {
        display: none;
    }
}

.header ul {
    list-style: none;
}

.header-l {
    display: flex;
    align-items: center;
    font-size: 1rem;
}

.header-r {
    display: flex;
    align-items: center;
}

.header li {
    float: left;
    margin-left: 60px;
    cursor: pointer;
}

.header li:hover {
    color: #409EFF;
}

.drop-item {
    color: #000;
    font-size: 1rem;
}

.more-btn {
    font-size: 25px;
    color: #000;
    cursor: pointer;
}

::v-deep .el-dialog__body {
    padding: 0px !important;
}

::v-deep .el-dialog {
    margin-top: 8vh !important;
}

.mobile-h-r {
    position: relative;
}

.mobile-menu {
    position: absolute;
    top: 100%;
    right: 0;
    background: white;
    width: 200px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
    padding: 10px 0;
    margin: 0;
    border-radius: 4px;
}

.mobile-menu li {
    float: none;
    margin: 0;
    padding: 10px 20px;
    text-align: left;
}

.mobile-menu li:hover {
    background-color: #f5f7fa;
}

.slide-fade-enter-active {
    transition: all .3s ease;
}

.slide-fade-leave-active {
    transition: all .3s ease;
}

.slide-fade-enter,
.slide-fade-leave-to {
    transform: translateY(-10px);
    opacity: 0;
}
</style>